﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CardGamesLibrary.Cards;
using CardGamesLibrary.Rules;

namespace CardGamesLibrary.Solitaire
{
    /// <summary>
    /// this rules shows how to limit the cards that can be added to a pile.
    /// this rule is for the piles a the top of the solitaire game.
    /// these piles can only start with an Ace and needs then to count up one by one in the same suite.
    /// </summary>
    public class SingleSolitairePileRule : ICardRule<FrenchDeckCard>
    {
        #region ICardRule<FrenchDeckCard> Members

        public bool CanAddCard(ICardPile<FrenchDeckCard> pile, FrenchDeckCard card)
        {
            // An empty pile allows any Ace
            if (pile.Count == 0 && card.Rank == Rank.Ace)
                return true;

            var lastCardOnPile = pile.Last();

            // no last card, our pile is empty and the card is not an ace, can't add
            if (lastCardOnPile == null)
                return false;

            // is this the last card eq. to the one we add - 1 
            // for ex. 5 equals to 6 - 1. if its true we can add the card
            var rank = (int)lastCardOnPile.Rank == ((int)card.Rank - 1) && card.Suite == lastCardOnPile.Suite;
            return rank;  
        }

        #endregion
    }
}
